import type { HandleMap } from "#connect/common/type.js"
import { type Logger, makeLogger } from "#logger"
import type * as Common from "../project/common.js"
import * as Philia from "../project/philia.js"
import API from "./api.js"
import LoggerManager from "./logger.js"
import NoticeManager from "./notice.js"
import type * as type from "./type.js"

export default class Manager {
  config: type.ManagerConfig = {
    logger: {
      max_lines: 1e4,
    },
    philia: {
      name: "Philia",
      type: "Socket",
      role: "Server",
      path: "Manager",
    },
  }
  logger: Logger
  logger_manager: LoggerManager
  handle: ReturnType<typeof API>
  philia: Philia.Project
  notice = new NoticeManager(this)

  constructor(
    public project: Common.Project,
    config: Common.IConfig["manager"],
  ) {
    Object.assign(this.config.logger, config?.logger)
    if (config?.philia) this.config.philia = config.philia

    this.logger = makeLogger("Manager", this.config.logger.level, this.config.logger.inspect)
    this.logger_manager = new LoggerManager(this)

    this.handle = API(this)
    this.philia = new Philia.Project(this.config.philia, this.handle as unknown as HandleMap)
    this.philia.logger = this.logger
  }

  start() {
    this.logger.info(`${this.project.config.name} 启动中……`)
    return Promise.all([this.philia.start(), this.project.start()]).then(i => {
      this.logger.info(`${this.project.config.name} 启动完成`)
      return i
    })
  }

  stop() {
    this.logger.info(`${this.project.config.name} 关闭中……`)
    return Promise.all([this.project.stop(), this.philia.stop()]).then(i => {
      this.logger.info(`${this.project.config.name} 关闭完成`)
      return i
    })
  }
}
